<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $fileName = urldecode($_POST['fileName']);
    $data = urldecode($_POST['data']);

    $filePath = "./" . $fileName;

    if (file_exists($filePath)) {
        $fileData = file_get_contents($filePath);
        $jsonData = json_decode($fileData, true);
    } else {
        $jsonData = array();
    }

    $postData = json_decode($data, true);

    if (isset($postData['bwnc'])) {
        $bwncData = $postData['bwnc'];

        foreach ($bwncData as $key => $value) {
            if (!isset($jsonData['bwnc'][$key])) {
                $jsonData['bwnc'][$key] = $value;
            }else{
            $jsonData['bwnc'][$key] = $value;
            }
        }
    }

    file_put_contents($filePath, json_encode($jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

    $response = array('status' => 'success');
    echo json_encode($response, JSON_UNESCAPED_UNICODE);
} else {
    echo "Invalid request method.";
}
?>
